/*
 * BncsClient.h
 *
 *  Created on: Nov 18, 2008
 *      Author: nathan
 */

#ifndef BNCSCLIENT_H_
#define BNCSCLIENT_H_

#include <QtGlobal>
#include <QtCore>
#include <QtNetwork>
#include "BncsMessage.h"
#include "ServiceAddress.h"

class BncsClient : public QObject
{
  Q_OBJECT;

 private:
  /* The server socket. */
  QTcpSocket* sock;
  QDataStream ds;

  /* Message processing state. */
  quint8 curMsgId;
  quint16 curMsgLen;

 public:
  BncsClient(QObject* parent);
  ~BncsClient();

 public:
  ServiceAddress getServiceAddress();

 signals:
  void connected();
  void messageReceived(BncsMessage message);
  void disconnected();
  void error(QString message);

 public slots:
  bool connect(ServiceAddress sa);
  bool sendMessage(BncsMessage message);
  bool disconnect();

 private slots:
  void sockError(QAbstractSocket::SocketError err);
  void sockResolve();
  void sockConnect();
  void sockReadReady();
  void sockDisconnect();
};

#endif /* BNCSCLIENT_H_ */

/* This stuff will be moved in to plugins later. */

/*
  void* context;

  QString host;
  quint16 port;
  QString user;
  QString pass;

  QString channel;
  QString lobbyName;

  // Session data
  SessionState state;
*/

/*
  public:
  enum LoginResult
  {
  LOGIN_SUCCESS,
  LOGIN_FAILURE,
  LOGIN_FAILURE_ACCOUNT_NOEXIST,
  LOGIN_FAILURE_ACCOUNT_UPGRADE,
  LOGIN_FAILURE_ACCOUNT_BADPASSWORD,
  LOGIN_FAILURE_ACCOUNT_NEEDSEMAIL,
  LOGIN_FAILURE_VERSION_UPGRADE,
  LOGIN_FAILURE_VERSION_DOWNGRADE,
  LOGIN_FAILURE_VERSION_INVALID,
  LOGIN_FAILURE_KEY1_INVALID,
  LOGIN_FAILURE_KEY1_INUSE,
  LOGIN_FAILURE_KEY1_BANNED,
  LOGIN_FAILURE_KEY1_WRONGPRODUCT,
  LOGIN_FAILURE_KEY2_INVALID,
  LOGIN_FAILURE_KEY2_INUSE,
  LOGIN_FAILURE_KEY2_BANNED,
  LOGIN_FAILURE_KEY2_WRONGPRODUCT
  };

  enum LobbyStartResult
  {
  LOBBY_START_SUCCESS,
  LOBBY_START_FAILURE
  };

  private:
  enum SessionState
  {
  SS_DISCONNECTED,
  SS_LOGIN,
  SS_INCHAT,
  SS_INLOBBY
  };

  enum ChatEventID
  {
  EID_SHOWUSER = 0x01,
  EID_JOIN = 0x02,
  EID_LEAVE = 0x03,
  EID_WHISPER = 0x04,
  EID_TALK = 0x05,
  EID_BROADCAST = 0x06,
  EID_CHANNEL = 0x07,
  EID_USERFLAGS = 0x09,
  EID_WHISPERSENT = 0x0a,
  EID_CHANNELFULL = 0x0d,
  EID_CHANNELDOESNOTEXIST = 0x0e,
  EID_CHANNELRESTRICTED = 0x0f,
  EID_INFO = 0x12,
  EID_ERROR = 0x13,
  EID_EMOTE = 0x17
  };

*/

/*
  void loginStarted(BncsClient* client);
  void loginAborted(BncsClient* client, QString reason);
  void loginComplete(BncsClient* client);

  void chatJoinChannel(BncsClient* client, QString channel);
  void chatJoin(BncsClient* client, QString user, QStringList flags, bool existing);
  void chatLeave(BncsClient* client, QString user);
  void chatFlagChange(BncsClient* client, QString user, QStringList flags);
  void chatWhisper(BncsClient* client, QString user, QString text);
  void chatWhisperSent(BncsClient* client, QString user, QString text);
  void chatTalk(BncsClient* client, QString user, QString text);
  void chatEmote(BncsClient* client, QString user, QString text);
  void chatBroadcast(BncsClient* client, QString user, QString text);
  void chatInfo(BncsClient* client, QString text);
  void chatError(BncsClient* client, QString text);

  void lobbyStarted(BncsClient* client);
  void lobbyAborted(BncsClient* client, QString reason);
  void lobbyComplete(BncsClient* client);

  void logoutStarted(BncsClient* client);
  void logoutAborted(BncsClient* client, QString reason);
  void logoutComplete(BncsClient* client);
*/

/*
  public slots:
  void startLogin(QString host, quint16 port, QString user, QString pass);
  void sendChat(QString message);
  void sendEmote(QString message);
  void sendWhisper(QString message, QString target);
  void joinChannel(QString channel);
  void startLobby(QString name);
  void endLobby();
  void startLogout();
*/

/*
  void processBncsPing(QDataStream& ds);
  void processBncsAuthInfo(QDataStream& ds);
  void processBncsCheck(QDataStream& ds);
  void processBncsAccountLogon(QDataStream& ds);
  void processBncsAccountLogonProof(QDataStream& ds);
  void processBncsEnterChat(QDataStream& ds);
  void processBncsChatEvent(QDataStream& ds);
*/
